package com.klod.news.viewmodel

import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.compose.LazyPagingItems
import androidx.paging.map
import com.klod.news.config.AppConfig.NETWORK_PAGE_SIZE
import com.klod.news.config.RouteError
import com.klod.news.config.RouteMain
import com.klod.news.data.NetworkResult
import com.klod.news.data.bean.BannerBean
import com.klod.news.data.bean.NewsDetailBean
import com.klod.news.data.bean.NewsTypeBean
import com.klod.news.data.reponsitory.HomeListDs
import com.klod.news.data.reponsitory.HomeRepository
import com.klod.news.ext.launch
import com.klod.news.utils.RouteManage
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import org.koin.core.annotation.Single
import org.koin.java.KoinJavaComponent.inject


@Single
class MainActivityVm(private val homeRepository: HomeRepository) : ViewModel() {
    //banner数据
    val bannerData =
        MutableStateFlow<NetworkResult<MutableList<BannerBean>>>(NetworkResult.Loading())

    val newsTypeData =
        MutableStateFlow<NetworkResult<MutableList<NewsTypeBean>>>(NetworkResult.Loading())

    //当前选择的tab
//    private val _newsTypeSelect = MutableSharedFlow<NewsTypeBean>()
    val newsTypeSelect: MutableStateFlow<NewsTypeBean> =
        MutableStateFlow(NewsTypeBean("1000", "推荐"))

  /*  private val _state = MutableSharedFlow<PagingData<NewsDetailBean>>(
        replay = 1,
   onBufferOverflow = BufferOverflow.DROP_OLDEST
    )
    val state: Flow<PagingData<NewsDetailBean>> by lazy { _state.distinctUntilChanged() }

*/
    init {
//        viewModelScope.launch {
//            newsTypeSelect.emit(NewsTypeBean("1000", "推荐")) // 手动发送初始值
//        }
        getBannerData()
        getNewsTypeData()
    }

    //定义路由
    private var _routeState =
        MutableStateFlow<RouteManage>(RouteManage.JumpTo(RouteMain))

    val routeState: StateFlow<RouteManage> = _routeState.asStateFlow()

//    val routeState: MutableStateFlow<RouteManage> = flow {
//        emit(RouteManage.JumpTo(ErrorRoute))
//    }.mutableStateIn(
//        viewModelScope,
//        RouteManage.JumpTo(ErrorRoute)
//    )

    fun setRoute(rm: RouteManage) {
        launch {
            _routeState.value = rm
        }


    }
    val newsList: Flow<PagingData<NewsDetailBean>>
        get() = homeRepository.getHomeList()
            .cachedIn(viewModelScope)
//            .map { pagingList ->
//                pagingList.map {
//                    NewsDetailBean(
//                        id = it.id,
//                        title = it.title,
//                        type = it.poster
//                    )
//                }
//            }

    //    val bannerData: StateFlow<NetworkResult<MutableList<BannerBean>>> = flow {
//        homeRepository.getHomeBanner().collect { result ->
//            emit(result)
//        }
//    }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), NetworkResult.Loading())
//



    fun getBannerData() {
        launch {
            homeRepository.getHomeBanner().collect { result ->
                bannerData.emit(result)
            }
        }
    }



    fun getNewsTypeData() {
        launch {
            homeRepository.getNewsBType().collect { result ->
                newsTypeData.emit(result)
            }
        }
    }

    fun tabSelect(bean: NewsTypeBean) {
        viewModelScope.launch {
            newsTypeSelect.emit(bean)
        }
    }
}